//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "LmCorrelationModel.h"
using namespace Cephei::QL::Legacy::Libormarketmodels;
#include <gen/QL/Math/Array.h>
#include <gen/QL/Models/Parameter.h>
using namespace Cephei::QL::Math;
using namespace Cephei::QL::Models;
#undef HANDLE
#define ABSTRACT
#undef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::CLmCorrelationModel (boost::shared_ptr<QuantLib::LmCorrelationModel>& childNative, Object^ owner) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmCorrelationModel = NULL;
#endif
	_ppLmCorrelationModel = &childNative;
    
}
Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::CLmCorrelationModel (QuantLib::LmCorrelationModel& childNative, Object^ owner) 
{
#ifdef HANDLE
	_phLmCorrelationModel = NULL;
#endif
	_ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (&childNative);
    
    _LmCorrelationModelOwner = owner;
    
}

Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::CLmCorrelationModel (CLmCorrelationModel^ copy) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmCorrelationModel = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (copy->GetShared());
        
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::CLmCorrelationModel (System::Type^ t) 
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmCorrelationModel = NULL;
#endif
	if (!t->IsSubclassOf(CLmCorrelationModel::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::CLmCorrelationModel (QuantLib::Handle<QuantLib::LmCorrelationModel>& childNative, Object^ owner)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLmCorrelationModel = &childNative;
	_ppLmCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmCorrelationModel>>(childNative.currentLink());
    
    _LmCorrelationModelOwner = owner;
}
Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::CLmCorrelationModel (QuantLib::Handle<QuantLib::LmCorrelationModel> childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
	_phLmCorrelationModel = &childNative;
	_ppLmCorrelationModel = &static_cast<boost::shared_ptr<QuantLib::LmCorrelationModel>>(childNative.currentLink());
    
}
#endif
#ifdef STRUCT
Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::CLmCorrelationModel (QuantLib::LmCorrelationModel childNative)  
{
	_pSpinlock = new boost::detail::spinlock ();
#ifdef HANDLE
	_phLmCorrelationModel = NULL;
#endif
	_ppLmCorrelationModel = new boost::shared_ptr<QuantLib::LmCorrelationModel> (new QuantLib::LmCorrelationModel (childNative));
    
}
#endif

Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::~CLmCorrelationModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
        _pSpinlock = NULL;
    }
    if (_ppLmCorrelationModel != NULL)
    {
	    delete _ppLmCorrelationModel;
        _ppLmCorrelationModel = NULL;
    }
}
Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::!CLmCorrelationModel ()
{
	if (_pSpinlock != NULL)
    {
        delete _pSpinlock;
    }
    if (_ppLmCorrelationModel != NULL)
    {
	    delete _ppLmCorrelationModel;
    }
}
QuantLib::LmCorrelationModel& Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::GetReference ()
{
    if (_ppLmCorrelationModel == NULL) throw gcnew NativeNullException ();
	return **_ppLmCorrelationModel;
}
boost::shared_ptr<QuantLib::LmCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::GetShared ()
{
    if (_ppLmCorrelationModel == NULL) throw gcnew NativeNullException ();
	return *_ppLmCorrelationModel;
}
QuantLib::LmCorrelationModel* Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::GetPointer ()
{
    if (_ppLmCorrelationModel == NULL) throw gcnew NativeNullException ();
	return &**_ppLmCorrelationModel;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::LmCorrelationModel>& Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::GetHandle ()
{
	if (_phLmCorrelationModel == NULL)
	{
		_phLmCorrelationModel = new Handle<QuantLib::LmCorrelationModel> (*_ppLmCorrelationModel);
	}
	return *_phLmCorrelationModel;
}
#endif
bool Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::HasNative () 
{
	return (_ppLmCorrelationModel != NULL);
}

Double Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::Correlation (UInt64 i, UInt64 j, Double t, Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>^ x)
{
    CArray^ _Cx;
    try
    {
        QuantLib::Size _i = (QuantLib::Size)ValueHelper::Convert (i);
        QuantLib::Size _j = (QuantLib::Size)ValueHelper::Convert (j);
        QuantLib::Time _t = (QuantLib::Time)ValueHelper::Convert (t);
        if (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x))
        {
            _Cx = safe_cast<CArray^> (x->Value);
            _Cx->Lock();
        }
        QuantLib::Array& _x = 
            (Microsoft::FSharp::Core::FSharpOption<Cephei::QL::Math::IArray^>::IsSome::get (x) ? static_cast<QuantLib::Array&> (_Cx->GetReference ()) : QuantLib::Null<QuantLib::Array>()); //6
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppLmCorrelationModel)->correlation ( _i,  _j,  _t,  _x );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Cx != nullptr) _Cx->Unlock();
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::Factors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLmCorrelationModel)->factors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Boolean Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::IsTimeIndependent::get ()
{
    try
    {
    	bool _rv = (bool)(*_ppLmCorrelationModel)->isTimeIndependent ( );   
        Boolean _nrv = (Boolean)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::IVector<Cephei::QL::Models::IParameter^>^ Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::P_params::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	std::vector<QuantLib::Parameter>& _rv = (std::vector<QuantLib::Parameter>&)(*_ppLmCorrelationModel)->params ( );   
        CoVector<Cephei::QL::Models::IParameter^>^ _nrv = gcnew CoVector<Cephei::QL::Models::IParameter^>(gcnew CParameterVector (_rv, this));
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Cephei::QL::Legacy::Libormarketmodels::ILmCorrelationModel^ Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::SetParams (Cephei::IVector<Cephei::QL::Models::IParameter^>^ arguments)
{
    CoVector<Cephei::QL::Models::IParameter^>^ _Carguments;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Carguments = safe_cast<CoVector<Cephei::QL::Models::IParameter^>^> (arguments);
        _Carguments->Lock();
        INativeVector<Cephei::QL::Models::IParameter^>^ _NCIarguments = _Carguments->getFeature (NativeFeature::Value);
        CParameterVector^ _NCarguments = safe_cast<CParameterVector^>(_NCIarguments);
        std::vector<QuantLib::Parameter>& _arguments = static_cast<std::vector<QuantLib::Parameter>&> (_NCarguments->GetReference ());
    	(*_ppLmCorrelationModel)->setParams ( _arguments );
    	return this;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Carguments != nullptr) _Carguments->Unlock();
    }
}
UInt64 Cephei::QL::Legacy::Libormarketmodels::CLmCorrelationModel::Size::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppLmCorrelationModel)->size ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

